﻿#region << 文件说明 >>

/*----------------------------------------------------------------
 * 命名空间：Vampirewal.Core.WebApi.Attributes
 * 唯一标识：c0bb020f-1c9d-4396-bd4c-8bd9947d3602
 * 文件名：VampirewalActionFilterAttribute
 *
 * 创建者：杨程
 * 电子邮箱：235160615@qq.com
 * 创建时间：2023/10/9 13:24:07
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/

#endregion << 文件说明 >>

namespace Vampirewal.Core.WebApi;

/// <summary>
/// 方法拦截器
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public abstract class VampirewalActionFilterAttribute : Attribute, IVampirewalActionFilter
{
    /// <summary>
    /// 构造函数
    /// </summary>
    public VampirewalActionFilterAttribute()
    {
    }

    /// <summary>
    /// 执行
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public abstract Task<bool> ExcuteActionAsync(IVampirewalActionFilterContext context, Func<Task<bool>> next);
}

/// <summary>
/// 方法拦截器
/// </summary>
public interface IVampirewalActionFilter
{
    /// <summary>
    /// 执行
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    Task<bool> ExcuteActionAsync(IVampirewalActionFilterContext context, Func<Task<bool>> next);
}


/// <summary>
/// 方法拦截器上下文
/// </summary>
public interface IVampirewalActionFilterContext
{
    /// <summary>
    /// Http请求和返回值的上下文
    /// </summary>
    HttpListenerContext HttpContext { get; internal set; }

    /// <summary>
    /// 控制器类型
    /// </summary>
    Type ControllerType { get; internal set; }

    /// <summary>
    /// 执行的方法
    /// </summary>
    MethodInfo Method { get; internal set; }

    /// <summary>
    /// 方法中的参数
    /// </summary>
    object[] Paramter { get; internal set; }

    /// <summary>
    /// 错误信息
    /// </summary>
    object ErrorInfo { get;internal set; }

    /// <summary>
    /// 是否执行成功
    /// </summary>
    bool IsSuccess { get; }

}

/// <summary>
/// 方法拦截器上下文
/// </summary>
internal sealed class VampirewalActionFilterContext : IVampirewalActionFilterContext
{
    public VampirewalActionFilterContext(HttpListenerContext _context, Type _controllerType, MethodInfo _method, params object[] _paramter)
    {
        HttpContext = _context;
        ControllerType = _controllerType;
        Method = _method;
        Paramter = _paramter;
    }

    /// <inheritdoc/>
    public HttpListenerContext HttpContext { get;  set; }

    /// <inheritdoc/>
    public Type ControllerType { get;  set; }

    /// <inheritdoc/>
    public MethodInfo Method { get;  set; }

    /// <inheritdoc/>
    public object[] Paramter { get;  set; }

    /// <inheritdoc/>
    public object ErrorInfo { get;  set; }

    public bool IsSuccess
    {
        get
        {
            return ErrorInfo == null;
        }
    }
}